#!/bin/bash
###这是数据库用户添加脚本
read -p "请输入你的ROOT用户密码" dbpass
read -p "是否确定新建数据库用户[y/n]" new
while [ $new == y ];do
	if [ $new == y ];then
		read -p "请输入新用户名称" dbuser
		read -p "请输入新用户的密码" dbpassFORblog
		read -p "是否新建数据库[y/n]" newdb
		if [ $newdb == y ];then
			read -p "请输入新的数据库名称" dbnameFORblog
			mysql -uroot -p$dbpass -e "create database $dbnameFORblog;"
		else
			read -p "请输入已存在的数据库名称用于该新用户使用" dbnameFORblog
		fi
		mysql -uroot -p$dbpass -e "create user $dbuser ;"
		mysql -uroot -p$dbpass -e "flush privileges;"
		mysql -uroot -p$dbpass -e "GRANT ALL PRIVILEGES ON $dbnameFORblog.* TO '$dbuser'@'localhost' IDENTIFIED BY '$dbpassFORblog' WITH GRANT OPTION;"
		read -p "是否开启远程访问？[y/n]" y
		if [ $y == y ];then
				mysql -uroot -p$dbpass -e "GRANT ALL PRIVILEGES ON $dbnameFORblog.* TO '$dbuser'@'%' IDENTIFIED BY '$dbpassFORblog' WITH GRANT OPTION;"
		else
			echo -e '正在配置'
		fi
		mysql -uroot -p$dbpass -e "flush privileges;"
		read -p "是否继续添加用户,输入 y 或者  n " new
	else
		echo -e '跳出用户配置'
	fi
break;
done
